package MypageController.MyWrite;

import java.text.SimpleDateFormat;

import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.ResultSet;

import Connect.connector;

public class MyWriteDAO {
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	public void entrieSet(MyWriteBean mywritebean){
		conn = connector.getConnection();
		try{
			
			String sql = "INSERT INTO ENTRIES (`Blogs_Id`, `Ent_Title`, `Ent_Contents`, `Ent_Nick`, `Ent_DateString`, `Ent_Date`, `Ent_Kategorie`) VALUES (?, ?, ?, ?, Sysdate(), Sysdate(), ?)";
			
			pstmt = conn.prepareStatement( sql );
			
			pstmt.setInt(1, mywritebean.getBlogs_Id());	
			pstmt.setString(2, mywritebean.getEnt_Title());
			pstmt.setString(3, mywritebean.getEnt_Contents());
			pstmt.setString(4, mywritebean.getEnt_Nick());
			pstmt.setString(5, mywritebean.getEnt_Kategorie());
			
			pstmt.executeUpdate();	
			
		}catch(Exception e){
			 e.printStackTrace();
		}finally{
			connector.releaseConnection(conn, pstmt, rs);
		}
	}
	
	public void entrieUpdate(MyWriteBean mywritebean){
		conn = connector.getConnection();
		try{
			
			
			String sql = "UPDATE ENTRIES SET `Ent_Title`=?, `Ent_Contents`=?, `Ent_Kategorie`=? WHERE `ID`=?";
			
			pstmt = conn.prepareStatement( sql );
			
			pstmt.setString(1, mywritebean.getEnt_Title());	
			pstmt.setString(2, mywritebean.getEnt_Contents());
			pstmt.setString(3, mywritebean.getEnt_Kategorie());
			pstmt.setInt(4, mywritebean.getID());
			
			pstmt.executeUpdate();	
			
		}catch(Exception e){
			 e.printStackTrace();
		}finally{
			connector.releaseConnection(conn, pstmt, rs);
		}
	}
	
	public MyWriteBean entrieSee(int id){
		conn = connector.getConnection();
		MyWriteBean muwritevean = new MyWriteBean();
		try{
			
			
			String sql = "select * from ENTRIES where ID = ?";
			
			pstmt = conn.prepareStatement( sql );
			
			pstmt.setInt(1, id);	
			
			rs = pstmt.executeQuery();	
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			while(rs.next()){
				
				muwritevean.setID(rs.getInt("ID"));
				muwritevean.setBlogs_Id(rs.getInt("Blogs_Id"));
				muwritevean.setEnt_Title(rs.getString("Ent_Title"));
				muwritevean.setEnt_Contents(rs.getString("Ent_Contents"));
				muwritevean.setEnt_Nick(rs.getString("Ent_Nick"));
				muwritevean.setEnt_Kategorie(rs.getString("Ent_Kategorie"));
				muwritevean.setEnt_DateString(sdf.format(rs.getTimestamp("Ent_DateString")));
				muwritevean.setEnt_Date(rs.getDate("Ent_Date"));
				muwritevean.setEnt_ComCount(rs.getInt("Ent_ComCount"));
				muwritevean.setEnt_Number(rs.getInt("Ent_Number"));

			}
		}catch(Exception e){
			 e.printStackTrace();
		}finally{
			connector.releaseConnection(conn, pstmt, rs);
		}
		return muwritevean;
	}
	
	public void entrieDelet(int mypageid, int entriesid){
		conn = connector.getConnection();
		try{
		
			String sql = "DELETE FROM `ENTRIES` WHERE (`ID`=?)";
			pstmt = conn.prepareStatement( sql );
			pstmt.setInt(1, entriesid);	
			pstmt.executeUpdate();	
			
			
			sql = "DELETE FROM `COMMENTBLOGS` WHERE Blogs_Id=? && Entries_Id=?";
			pstmt = conn.prepareStatement( sql );
			pstmt.setInt(1, mypageid);	
			pstmt.setInt(2, entriesid);
			pstmt.executeUpdate();
			
		}catch(Exception e){
			 e.printStackTrace();
		}finally{
			connector.releaseConnection(conn, pstmt, rs);
		}
	}

}
